package org.alala.datamodels.common.query;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.google.common.base.Preconditions;

public class Query implements Serializable {

	private static final long serialVersionUID = 8655974387613374353L;

	private List<QueryItem> queryItems = new ArrayList<QueryItem>();

	private Integer limit;

	private Integer offset;
	
	private String order;

	private String orderDir = "ASC";

	private Long userId;
	
	private int type;
	
	private Long id;
	
	private boolean brief;
	
	private List<OrderBy> sorts;
	
	
	public Query(){
		
	}
	public Query(Integer page, Integer pageSize){
		
		if(page == null || pageSize == null){
			
		}
		else{
			this.offset = page * pageSize;
			this.limit = pageSize;
		}
		
	}

	public void addQueryItem(QueryItem queryItem) {		
		
		Preconditions.checkNotNull(queryItem,"query item cannot be null");
		Preconditions.checkNotNull(queryItem.isValid(),"query item is not valid");		

		this.queryItems.add(queryItem);
	}

	public void clearQueryItems() {

		this.queryItems.clear();
	}

	public List<QueryItem> getQueryItems() {

		return queryItems;
	}

	public void setQueryItems(List<QueryItem> queryItems) {

		this.queryItems = queryItems;
	}

	public Integer getOffset() {

		return offset;
	}

	public void setOffset(Integer offset) {

		this.offset = offset;
	}

	public Integer getLimit() {

		return limit;
	}

	public void setLimit(Integer limit) {

		this.limit = limit;
	}

	public String getOrder() {

		return order;
	}

	public void setOrder(String order) {

		this.order = order;
	}

	public String getOrderDir() {

		return orderDir;
	}

	public void setOrderDir(String orderDir) {

		this.orderDir = orderDir;
	}

	public Long getUserId() {

		return userId;
	}

	public void setUserId(Long userId) {

		this.userId = userId;
	}

	public boolean isBrief() {

		return brief;
	}

	public void setBrief(boolean brief) {

		this.brief = brief;
	}
	public int getType() {

		return type;
	}
	public void setType(int type) {

		this.type = type;
	}
	public Long getId() {

		return id;
	}
	public void setId(Long id) {

		this.id = id;
	}
	public List<OrderBy> getSorts() {

		return sorts;
	}
	public void setSorts(List<OrderBy> sorts) {

		this.sorts = sorts;
	}

	public static Query newQueryWithId(long id) {
		Query query = new Query();
		query.setId(id);
		return query;
	}
	
}
